<%@ page import="net.sf.json.JSONObject" %>
<%@ page language="java" contentType="text/html; charset=utf-8" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <meta charset="utf-8">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <title><spring:message code="share"/></title>
    <script type="text/javascript">
		var basePath = '${basePath}';
	</script>	
    <meta id="viewport" name="viewport"
          content="width=device-width,initial-scale=1.0,minimum-scale=1.0,maximum-scale=1.0">
    <meta name="format-detection" content="telephone=no">
    <link href="<c:url value='/resource/css/main.css?234' />" type="text/css" rel="stylesheet">
    <link href="<c:url value='/resource/css/iconfont.css' />" type="text/css" rel="stylesheet">
    <%
        JSONObject user = (JSONObject) request.getAttribute("user");
    %>
</head>

<body>
<div class="container wj-share-container" id="share-container">
    <header class="module-topbar">
        <a class="fl" href="javascript:history.back();"><i class="iconfont">&#xe608;</i><spring:message code="attention"/></a>
		<%--<span><spring:message code="share"/></span> --%>
        <a class="fr" href="" id="writeShare"><spring:message code="writeShare"/>
        <%--<i class="iconfont add">&#xe607;</i><i class="iconfont add hid">&#xe609;</i>--%>
        </a>
    </header>
    <div class="module-editer">
        <div class="layout-box">
            <a class="box-col line-separate" href="write.jsp">
                <div class="icon character txt"><spring:message code="text"/></div>
            </a>
            <a class="box-col line-separate" href="write.jsp">
                <div class="icon picture txt"><spring:message code="photo"/></div>
            </a>
            <a class="box-col line-separate" href="javascript:;">
                <div class="icon video txt"><spring:message code="video"/></div>
            </a>
        </div>
    </div>
    <a class="module-message-tip" href="javascript:toMessages()">
        <spring:message code="news"/>
    </a>

    <div id="wapper-pull" class="margin-topbar">
        <div class="module module-user wj-module-user">
            <div href="javascript:;" class="background" style="background-image:url(${basePath}/resource/images/shareBg.jpg);background-size: 100% 100%;">
               <div class="avatar" uid="<%=user.optString("id")%>"><img src="<%=user.optString("avatar")%>" onerror="$(this).attr('src','http://123.57.249.184/i/defaultAvatar/moren_icon.png')"></div> 
            </div>
        </div>
        <div class="module-shares">
        </div>
    </div>
</div>
<script type="text/javascript" src="${basePath}/resource/js/common/messages_${sessionScope.lang}.js"></script>
<script type="text/javascript" src="${basePath}/resource/js/share/jquery.min.js"></script>
<script type="text/javascript" src="${basePath}/resource/js/share/layer/layer.m.js"></script>
<script type="text/javascript" src="${basePath}/resource/js/share/laytpl.js"></script>
<script type="text/javascript" src="${basePath}/resource/js/share/swipeSlide.js"></script>
<script type="text/javascript" src="${basePath}/resource/js/share/jquery.lazyload.min.js"></script>
<script type="text/javascript" src="${basePath}/resource/js/share/webuploader.html5only.min.js"></script>
<script type="text/javascript" src="${basePath}/resource/js/share/json2.js"></script>
<script type="text/javascript" src="${basePath}/resource/js/share/xpull.js"></script>
<script type="text/javascript" src="${basePath}/resource/js/share/share.js?20160128"></script>
<script src="${basePath}/resource/js/common/base.js"></script>
<script id="tpl" type="text/html">
    {{# for(var i = 0, len = d.length; i < len; i++){ var share = d[i];}}
    <div class="share line-around wj-line-around" id="{{ share.id }}" {{# if (share.type == '1'){ }}original="{{ share.original }}"{{# } }}>
		<jsp:include page="tpl/header.jsp" />
		<div class="wj-detail-box">
        	<jsp:include page="tpl/detail.jsp" />
        	<jsp:include page="tpl/interact.jsp" />
        	<jsp:include page="tpl/interactDetail.jsp" />
		</div>
    </div>
    {{# }  }}
</script>
<script>
    var feed;
    var offset = <%=request.getAttribute("offset")%>;
    var timestamp = '<%=request.getAttribute("timestamp")%>';
    var feeds = <%=request.getAttribute("feeds")%>;
    var next = <%=request.getAttribute("next")%>;
    <%-- var messageCount = <%=request.getAttribute("messageCount")%>; --%>
    var isMore = true;

    function writeWeibo() {
        $('.module-topbar .add').toggle();
        $(".module-editer").toggle();
    }

    function shareMessageTip() {
    	var lang = localStorage.getItem("lang");
    	var userToken = localStorage.getItem("userTonken");
    	var data = {
    		userId : userToken,
    		lang : lang,
    		userToken : userToken
		};
	   	var _action = userToken+"/message/count.do";
		base.AJAX_POST(_action, data, "json", function(o){
			if (o.data > 0) {
            	$('.module-message-tip').html(o.data + " " +messages.news).show();
        	}else{
        		$('.module-message-tip').hide();
        	}
		});
      
    }

    function toMessages() {
        //messageCount = 0;
        var userToken = localStorage.getItem("userTonken");
    	var lang = localStorage.getItem("lang");
        $('.module-message-tip').hide();
        window.location.href = Page.contextPath + '/message.do?userToken='+userToken+'&lang='+lang;
    }

    function loadShares(feeds, clear) {
        laytpl(document.getElementById('tpl').innerHTML).render(feeds, function (html) {
            var shares = $(html);
            shares.find('img.loading').lazyload({
                threshold : 200,
                effect: "fadeIn"
            });

            if (clear) {
                $('.module-shares').empty();
            }
            shares.appendTo($('.module-shares'));
            $(shares).each(function() {
                feed.bindShare($(this));
            });
            isMore = true;
        });
    }

    function reload() {
    	var userToken = localStorage.getItem("userTonken");
    	var lang = localStorage.getItem("lang");
        Page.ajaxGetQuiet('/feed/0.do?userToken='+userToken+'&lang='+lang, null, function (response) {
            timestamp = response.data.timestamp;
            offset = response.data.offset;
            //messageCount = response.data.messageCount;
            //shareMessageTip();
            Page.resetPull();
            
            // 加载分享
            loadShares(response.data.feeds, true);
        });
    }

    function CookieEnable() {
        var result = false;
        if (navigator.cookiesEnabled) {
            return true;
        }
        document.cookie = "testcookie=yes;";
        var cookieSet = document.cookie;
        if (cookieSet.indexOf("testcookie=yes") > -1) {
            result = true;
        }
        document.cookie = "";
        return result;
    }

    function nativeBack() {
        var browser = {
            versions : function() {
                var u = navigator.userAgent, app = navigator.appVersion;
                return {//移动终端浏览器版本信息
                    trident : u.indexOf('Trident') > -1, //IE内核
                    presto : u.indexOf('Presto') > -1, //opera内核
                    webKit : u.indexOf('AppleWebKit') > -1, //苹果、谷歌内核
                    gecko : u.indexOf('Gecko') > -1 && u.indexOf('KHTML') == -1, //火狐内核
                    mobile : !!u.match(/AppleWebKit.*Mobile.*/)
                    || !!u.match(/AppleWebKit/), //是否为移动终端
                    ios : !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/), //ios终端
                    android : u.indexOf('Android') > -1 || u.indexOf('Linux') > -1, //android终端或者uc浏览器
                    iPhone : u.indexOf('iPhone') > -1 || u.indexOf('Mac') > -1, //是否为iPhone或者QQHD浏览器
                    iPad : u.indexOf('iPad') > -1, //是否iPad
                    webApp : u.indexOf('Safari') == -1
                    //是否web应该程序，没有头部与底部
                };
            }(),
            language : (navigator.browserLanguage || navigator.language).toLowerCase()
        }
        if (browser.versions.ios || browser.versions.iPhone || browser.versions.iPad) {
            window.location.href = "ios:back";
        } else if (browser.versions.android) {
            native.back();
        }
    }

    $(function () {
    	
    	//alert($('.time').text());
    	var userToken = localStorage.getItem("userTonken");
    	var lang = localStorage.getItem("lang");
    	
    	var writeShareUrl = basePath + '/write.do' + "?userToken="+userToken+'&lang='+lang;
    	$('#writeShare').attr('href', writeShareUrl);

        if(!CookieEnable()){
            Page.notice("对不起，您的浏览器的Cookie功能被禁用，请开启");
        }

        shareMessageTip();

        Page.storeUser(<%=user%>);
        Page.storeToken('<%=request.getAttribute("_timestamp")%>', '<%=request.getAttribute("_nonce")%>', '<%=request.getAttribute("_digest")%>');

        feed = Feed.create($('.module-shares'), Share.create($('.module-publisher')));

        // 加载分享
        loadShares(feeds);

        Page.onPull(function () {
            reload();
        });

        Page.onMore(function () {
        	if(isMore){
	            if (next) {
	            	var userToken = localStorage.getItem("userTonken");
	            	var lang = localStorage.getItem("lang");
	            	isMore = false;
	                Page.ajaxGetQuiet('/feed/' + offset+".do?userToken="+userToken+'&lang='+lang, {timestamp: timestamp}, function (response) {
	                    // console.log(response)
	                    timestamp = response.data.timestamp;
	                    offset = response.data.offset;
	                    next = response.data.next;
	                    // 加载分享
	                    loadShares(response.data.feeds);
	                });
	            }
        	}
        });
        
        
        $("body").undelegate( "click" ).delegate(".goUserInfo","click",function(){
        	var userToken = localStorage.getItem("userTonken");
        	var lang = localStorage.getItem("lang");
        	var userName = $(this).text();
  	        userName = userName.replace(/\@/g, "")
  	        // n/名称.do
  	        var action = '${basePath}/n/'+ userName + '.do?userToken='+userToken+'&lang='+lang;
  	        window.location.href = action;
        });
        
      
    });
</script>

<jsp:include page="comment.jsp"/>
<jsp:include page="swipe.jsp"/>
</body>

</html>
